package com.org.gundutubruk;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class RemovePlayer extends Activity{
	
	TextView namaP;
	TextView gunduP;
	ImageView dispAvatar;
	Button kiri;
	Button kanan;
	Button hapus;
	
	int gbrApatar;
	int gbrNoPlayer;
	
	int currP;
	Context context;
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	setContentView(R.layout.remove_player_layout);
    	context=this;
    	gbrApatar=R.drawable.apatar0;
    	gbrNoPlayer=R.drawable.apatar;
    	castItem();
    	init();
    	isiFungsiItem();
    	
	}
	
	public void onBackPressed(){
		if(getIntent().getBooleanExtra("fromMultiplayer", false)) {//jika dari multiplayer
			finish();
		}else{
			this.startActivity(new Intent("android.intent.action.GAMESETTING"));
			overridePendingTransition(R.anim.fade, R.anim.hold);
			this.finish();
		}
	}
	
	private void castItem(){
		namaP=(TextView)findViewById(R.id.namaP);
		gunduP=(TextView)findViewById(R.id.gunduP);
		dispAvatar=(ImageView)findViewById(R.id.gbrapatar);
		kiri=(Button)findViewById(R.id.playerkiri);
		kanan=(Button)findViewById(R.id.playerkanan);
		hapus=(Button)findViewById(R.id.tmblhapus);
	}
	
	private void isiFungsiItem(){
		kiri.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currP>0){
					currP--;
					dispPlayerInfo();
				}
			}
		});
		kanan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currP<Utility.playerHolder.size()-1){
					currP++;
					dispPlayerInfo();
				}
			}
		});
		hapus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currP>-1){
					final Player P = Utility.playerHolder.get(currP);
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage("Apakah pemain "+P.name+" akan dihapus?")
					       .setCancelable(false)
					       .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                Utility.playerHolder.AllPlayer.remove(P);
					                Toast t = Toast.makeText(getApplicationContext(), "Berhasil dihapus", Toast.LENGTH_SHORT);
					                t.show();
					                currP--;
					                if(currP>-1){
					                	dispPlayerInfo();
					                }else if(Utility.playerHolder.size()>0){
					                	currP=0;
					                	dispPlayerInfo();
					                	
					                }else if(Utility.playerHolder.size()<=0){
					                	namaP.setText("---");
					        			gunduP.setText("---");
					        			dispAvatar.setImageResource(gbrNoPlayer);
					                }
					                Utility.serializePlayerHolder(getApplicationContext());
					           }
					       })
					       .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
				}
			}
		});
	}
	
	private void init(){
		if(Utility.playerHolder.size()>0){
			currP=0;
			dispPlayerInfo();
		}else{
			currP=-1;
			namaP.setText("---");
			gunduP.setText("---");
			dispAvatar.setImageResource(gbrNoPlayer);
		}
	}

	private void dispPlayerInfo() {
		Player P = Utility.playerHolder.get(currP);
		Log.d("", "nama : "+P.name);
		namaP.setText(P.name);
		gunduP.setText(""+P.jumlahGundu);
		//Log.d("", "angka : "+(gbrApatar+P.avatar));
		dispAvatar.setImageResource(gbrApatar+P.avatar);
	}
}
